<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2018/2/28
 * Time: 17:52
 */

namespace Resource\Service;
use Think\Model;

class UploadService extends Model
{
    protected $trueTableName = 'wz_upload';
    /**
     * @param $params
     * @return array|bool
     */
    public function addUpload($params) {
        $item_id = D('Resource/Upload')->addUpload($params);
        if($item_id) {
            return ['id'=>$item_id];
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getUploadInfo($params, $fields='') {
        $dataInfo = D('Resource/Upload')->getUploadInfo($params,$fields);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchUploadList($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        isset($params['name']) && $data['name'] = ['like',"%{$params['name']}%"];

        //查询项目总数
        $total = D('Resource/Upload')->getUploadCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询项目列表
        $dataList = D('Resource/Upload')->getUploadList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['dataList'] = $dataList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateUploadInfo($params) {
        return D('Resource/Upload')->updateUploadInfo($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function deleteUpload($params) {
        return D('Resource/Upload')->deleteUpload($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getAllUploadList($params){
        $dataInfo = D('Resource/Upload')->getAllUploadList($params);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return int
     */
    public function getUploadCount($params){
        $count = D('Resource/Upload')->getUploadCount($params);
        return intval($count);
    }

    /**
     * @param $params
     * @return array
     */
    public function getFileUploadObject($params){
        $p1 = $p2 = [];
        $base_path = '/Public/';
        $imgList = D('Resource/Upload')->getAllUploadList($params);
        if($imgList){
            foreach ($imgList as $k=>$v){
                $p1[$k] = $base_path.$v['file_path'];
                $p2[$k] = [
                    'caption' => $v['ext'],
                    // 要删除商品图的地址
                    'url' => "/admin/customer/ajaxDeleteFile",
                    // 商品图对应的商品图id
                    'key' => $v['id'],
                    //下载地址
                    'downloadUrl' => $base_path.$v['file_path'],
                ];
            }
        }
        return ['p1'=>$p1,'p2'=>$p2];
    }
}